package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.starlink.common.domain.BaseEntity;
import com.starlink.mapper.MallBooksMapper;
import com.starlink.model.domain.MallBooksDO;
import com.starlink.model.domain.SchoolbagBooksDO;
import com.starlink.service.SchoolbagBooksService;
import org.springframework.data.domain.Pageable;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.SchoolbagPhaseBooksDtoMapper;
import com.starlink.model.domain.SchoolbagPhaseBooksDO;
import com.starlink.mapper.SchoolbagPhaseBooksMapper;
import com.starlink.model.request.SchoolbagPhaseBooksRequest;
import com.starlink.service.SchoolbagPhaseBooksService;
import com.starlink.model.util.SchoolbagPhaseBooksConvert;
import com.starlink.model.vo.SchoolbagPhaseBooksVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import static com.starlink.utils.PageUtils.getOrderBy;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class SchoolbagPhaseBooksServiceImpl extends ServiceImpl<SchoolbagPhaseBooksMapper, SchoolbagPhaseBooksDO> implements SchoolbagPhaseBooksService {
    @Autowired
    private SchoolbagPhaseBooksMapper schoolbagPhaseBooksMapper;
    @Autowired
    private SchoolbagBooksService schoolbagBooksService;
    @Autowired
    private MallBooksMapper mallBooksMapper;

    @Override
    public SchoolbagPhaseBooksVO getByKey(long id) {
        SchoolbagPhaseBooksVO schoolbagPhaseBooksVO = SchoolbagPhaseBooksDtoMapper.INSTANCE.do2VO(getById(id));
        return schoolbagPhaseBooksVO;
    }

    @Override
    public PageInfo<SchoolbagPhaseBooksVO> pageQuery(Pageable pageable,SchoolbagPhaseBooksRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<SchoolbagPhaseBooksDO> list = schoolbagPhaseBooksMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<SchoolbagPhaseBooksVO> schoolbagPhaseBooksVOList = SchoolbagPhaseBooksConvert.buildSchoolbagPhaseBooksVoList(list);
        return new PageInfo(schoolbagPhaseBooksVOList);
    }

    @Override
    public List<SchoolbagPhaseBooksVO> listQuery(SchoolbagPhaseBooksRequest request) {
        List<SchoolbagPhaseBooksDO> list = schoolbagPhaseBooksMapper.selectDOList(SchoolbagPhaseBooksDtoMapper.INSTANCE.request2DO(request));
        List<SchoolbagPhaseBooksVO> schoolbagPhaseBooksVOS = SchoolbagPhaseBooksConvert.buildSchoolbagPhaseBooksVoList(list);
        for (SchoolbagPhaseBooksVO schoolbagPhaseBooksVO : schoolbagPhaseBooksVOS) {
            // 查询对应的书籍信息
            SchoolbagBooksDO schoolbagBooksDO = schoolbagBooksService.lambdaQuery().eq(BaseEntity::getId, schoolbagPhaseBooksVO.getBooksId()).last("limit 1").one();
            AssertUtil.assertNotNull(schoolbagBooksDO, "书包书籍不存在");
            // 书籍信息
            MallBooksDO mallBooksDO = mallBooksMapper.selectDOById(schoolbagBooksDO.getMallBooksId());
            AssertUtil.assertNotNull(mallBooksDO, "书包书籍关联的商城书籍不存在");
            if (mallBooksDO != null) {
                schoolbagPhaseBooksVO.setTitle(mallBooksDO.getTitle());
                schoolbagPhaseBooksVO.setPicture(mallBooksDO.getPicture());
                schoolbagPhaseBooksVO.setAuthor(mallBooksDO.getAuthor());
                schoolbagPhaseBooksVO.setPublishingHouse(mallBooksDO.getPublishingHouse());
            }
        }
        return schoolbagPhaseBooksVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(SchoolbagPhaseBooksRequest request) {
        save(SchoolbagPhaseBooksDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(SchoolbagPhaseBooksRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(SchoolbagPhaseBooksDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        schoolbagPhaseBooksMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }
}
